package com.cskaoyan.mall.payment.controller;

import com.alibaba.fastjson2.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.cskaoyan.mall.common.result.Result;
import com.cskaoyan.mall.order.dto.OrderInfoDTO;
import com.cskaoyan.mall.payment.client.OrderApiClient;
import com.cskaoyan.mall.payment.service.PayService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@Slf4j
public class PaymentController {
    @Autowired
    OrderApiClient orderApiClient;
    @Autowired
    PayService payService;

    @GetMapping("/pay/auth")
    public Result<OrderInfoDTO> payIndex(Long orderId) {
        // 调用订单服务获取订单信息
        OrderInfoDTO orderInfoDTO = orderApiClient.getOrderInfoDTO(orderId);
        return Result.ok(orderInfoDTO);
    }

    @GetMapping("/pay/alipay/submit/{orderId}")
    public String submitOrder(@PathVariable Long orderId) throws AlipayApiException {
        String aliPay = payService.createAliPay(orderId);
        return aliPay;
    }


    @PostMapping("/pay/alipay/callback/notify")
    @SneakyThrows
    public String callbackNotify(@RequestParam Map<String, String> paramsMap) {
        log.info("支付成功，异步回调，paramMap:{}", JSON.toJSONString(paramsMap));
        String result=payService.callbackNotify(paramsMap);


        return result;
    }
}